package com.example.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: TtlQueueConfig
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/26-16:52:00
 * Description:
 * 1: 声明两个交换机, 普通交换机和死信交换机
 * 2: 声明三个队列, 普通队列A, 普通队列B, 死信队列. 前两者的参数需要Map和死信队列绑定
 */
@Configuration
public class TtlQueueConfig {
    private static final String X_EXCHANGE = "X";
    private static final String QUEUE_A = "QA";
    private static final String QUEUE_B = "QB";

    private static final String Y_EXCHANGE_DEAD = "Y";
    private static final String Y_DEAD_QUEUE = "QD";

    /**
     * 升级版
     */
    private static final String QUEUE_C = "QC";

    /**
     * 声明xExchange交换机,类型为direct
     *
     * @return
     */
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明yExchange交换机,类型为direct
     *
     * @return
     */
    @Bean("yExchange")
    public DirectExchange yExchange() {
        return new DirectExchange(Y_EXCHANGE_DEAD);
    }

    /**
     * 让普通队列QA跟死信交换机进行绑定, 设置普通队列的TTL为10S;
     */
    @Bean("queueA")
    public Queue queueA() {
        Map<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_EXCHANGE_DEAD);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();
    }

    /**
     * 让普通队列QB跟死信交换机进行绑定, 设置普通队列的TTL为40S;
     */
    @Bean("queueB")
    public Queue queueB() {
        Map<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_EXCHANGE_DEAD);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();
    }

    /**
     * 让普通队列C跟死信交换机进行绑定, 此处不设置队列的TTL时间
     */
    @Bean("queueC")
    public Queue queueC() {
        Map<String, Object> args = new HashMap<>();
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", Y_EXCHANGE_DEAD);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
    }

    /**
     * 声明死信队列QD
     */
    @Bean("queueD")
    public Queue queueD() {
        return new Queue(Y_DEAD_QUEUE);
    }

    /**
     * 普通队列A跟普通交换机进行绑定, rountingKey为XA;
     */
    @Bean
    public Binding queueBindingA(@Qualifier("queueA") Queue queueA,
                                 @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    /**
     * 普通队列B跟普通交换机进行绑定, rountingKey为XB;
     */
    @Bean
    public Binding queueBindingB(@Qualifier("queueB") Queue queueB,
                                 @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    /**
     * 普通队列C跟普通交换机进行绑定, rountingKey为XC;
     */
    @Bean
    public Binding queueBindingC(@Qualifier("queueC") Queue queueC,
                                 @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

    /**
     * 死信队列和死信交换机进行绑定, rountingKey为YD;
     */
    @Bean
    public Binding deadLetterBindD(@Qualifier("queueD") Queue queueD,
                                   @Qualifier("yExchange") DirectExchange yExchange) {
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }

}
